package com.wx.activitys.drp.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.github.sd4324530.fastweixin.api.JsAPI;
import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.UserAPI;
import com.github.sd4324530.fastweixin.api.enums.OauthScope;
import com.github.sd4324530.fastweixin.api.response.GetSignatureResponse;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.api.response.OauthGetTokenResponse;
import com.wx.activitys.base.controller.BaseController;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.UserInfoService;

@Controller
@RequestMapping(value = "/enter")
public class EnController extends BaseController {
	@Autowired
	private OauthAPI authAPI;
	@Autowired
	private JsAPI jsAPI;
	@Autowired
	private UserAPI userAPI;
	@Autowired
	private UserInfoService userInfoService;

	public static final String ROOT_NAME = "drp";
	public static final String BASE_PATH = "http://jh.xuanzhen-tech.com";
	public static final String BASE_URL = BASE_PATH;
	public static final String REDIRECT_URL = BASE_URL + "/enter/dealAuthUser.html";

	@RequestMapping(value = "auth2.html", method = RequestMethod.GET)
	public void auth2(HttpSession session, HttpServletResponse response, HttpServletRequest request)
			throws IOException {
		String spread = request.getParameter("spread");
		String onOpenId = request.getParameter("onOpenId");
	    System.out.println(onOpenId+"auth2.html");
		String redirectURL = authAPI.getOauthPageUrl(REDIRECT_URL, OauthScope.SNSAPI_USERINFO, "STATE");
		session.setAttribute("spread", spread);
		session.setAttribute("onOpenId", onOpenId);
		response.sendRedirect(redirectURL);
	}

	@RequestMapping(value = "dealAuthUser.html", method = RequestMethod.GET)
	public String dealAuthUser(HttpSession session, HttpServletRequest request) {
		String code = request.getParameter("code");
		OauthGetTokenResponse authToken = authAPI.getToken(code);
		GetUserInfoResponse userInfo = authAPI.getUserInfo(authToken.getAccessToken(), authToken.getOpenid());
		GetUserInfoResponse userInfo1 = userAPI.getUserInfo(authToken.getOpenid());
		userInfo.setOpenid(authToken.getOpenid());
		userInfo.setSubscribe(userInfo1.getSubscribe());
		logger.info("openid为" + authToken.getOpenid());
		logger.info(userInfo);
		session.setAttribute("userInfo", userInfo);
		return ROOT_NAME + "/redirect";
	}

	@RequestMapping(value = "index.html", method = RequestMethod.GET)
	public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
		GetUserInfoResponse userInfo = ((GetUserInfoResponse) request.getSession().getAttribute("userInfo"));
		if (userInfo != null) {
			BUserInfo buserinfo = userInfoService.findByOpenid(userInfo.getOpenid());
			if (buserinfo != null) {
				if(buserinfo.getState()==0){
					return ROOT_NAME + "/error/error";
				}else{
					String url = "/home.html";
					model.addAttribute("url", url);
					return ROOT_NAME + "/redirect2";
				}
			} else {
				model.addAttribute("baseUrl", BASE_URL);
				String currentPage = BASE_URL + "/index.html";
				GetSignatureResponse signObj = jsAPI.getSignature(currentPage);
				model.addAttribute("signObj", signObj);
				model.addAttribute("basePath", BASE_PATH);
				return ROOT_NAME + "/register";
			}
		} else {
			return "redirect:/enter/auth2.html";
		}
	}
}
